package com.hzx.demo.view;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.hzx.demo.R;
import com.hzx.demo.util.HZXLog;

/**
 * Created by ke on 16-5-15.
 */
public class ImageViewPopupWindow{
    @SuppressWarnings("unused")
    private static final String TAG = "ImageViewPopupWindow";

    private PopupWindow mPopupWindow;
    private ImageViewTouch mImageViewTouch;

    public boolean isShowing(){
        return mPopupWindow == null ? false : mPopupWindow.isShowing();
    }

    public void show(final Activity activity, String url){
        if(mPopupWindow == null){
            mPopupWindow = new PopupWindow(activity);
            mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setClippingEnabled(false);
            mPopupWindow.setBackgroundDrawable(null);
            mImageViewTouch = new ImageViewTouch(activity);
        }

        if(mImageViewTouch.getParent() != null){
            ((ViewGroup)mImageViewTouch.getParent()).removeView(mImageViewTouch);
        }
        mPopupWindow.setContentView(mImageViewTouch);
        mPopupWindow.showAtLocation(activity.findViewById(android.R.id.content), Gravity.NO_GRAVITY, 0, 0);
        Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.raw.img);
        mImageViewTouch.setImageBitmap(bitmap);
        mImageViewTouch.setListener(new ImageViewTouch.ImageViewTouchListener() {
            @Override
            public void onSingleTap(ImageViewTouch imageViewTouch) {
                HZXLog.v(TAG, "onSingleTap");
                dismissPopupWindow();
            }
        });
    }

    public void dismissPopupWindow(){
        if(mPopupWindow == null) return;

        mImageViewTouch.recycleBitmap();
        mPopupWindow.dismiss();
        mImageViewTouch = null;
        mPopupWindow = null;
    }
}
